//外接订单列表

interface ICspOrderViewModel extends IBaseViewModel<ICspOrderQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<cspOrderListResponse>;
    /**
     * 订单状态下拉
     */
    orderStatusDropDown: ValueListItemResponse[];
    /**
     * 删除订单
     */
    deleteOrder: (id: string) => void;
    /**
     * 编辑订单
     */
    editCspOrder: (row: cspOrderListResponse) => void;
    /**
     * 动态列表高度
     */
    heightA:number;
}

interface ICspOrderQueryTerms {
    /**
     * 订单编号
     */
    orderId: string;
    /**
     * 客户单位
     */
    clientName: string;
    /**
     * 订单状态
     */
    states: string;
    /**
     * 发货起始时间
     */
    startTime: string;
    /**
     * 发货结束时间
     */
    endTime: string;
    /**
     * 发货地址
     */
    startAddress: string;
    /**
     * 送货地址
     */
    endAddress: string;
}

class CspOrderListController extends ListController<ICspOrderViewModel, ICspOrderQueryTerms> {
    gridApi: uiGrid.IGridApiOf<cspOrderListResponse>;
    orderBy: number = 0;
    asc: boolean = true;
    selectedId: string;

    constructor($scope: ICspOrderViewModel,private cspOrderService: IcspOrderService,
    private $state: angular.ui.IStateService, private $ngBootbox: BootboxService, private valueService: IValueService,private $location:ng.ILocationService,private exportService:IexportService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.$scope.editCspOrder = this.editCspOrder;
        this.$scope.$on("WildDog:Order21", this.onWilddogOrderEvent);

        $('#opendataTime').click(function () {
            $('#dataTime').datetimepicker('show');
        });
        $('#opendataTimeEnd').click(function () {
            $('#dataTimeEnd').datetimepicker('show');
        });

        this.$scope.queryTerms = {
            orderId: "",
            clientName: "",
            states: "0",
            startTime: "",
            endTime: "",
            startAddress: "",
            endAddress: ""
        };

        this.$scope.gridOptions = {
            enableSelectAll: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableFullRowSelection: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "订单编号", field: 'cspOrderId', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: "发货地址", field: 'startAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '货物数量', field: 'quantityOfGoods', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '发货发布时间', field: 'cspOrderTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: '紧急程度', field: 'responseTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '订单状态', field: 'status', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                {
                    displayName: "操作",
                    field: 'orderStatus',
                    cellTemplate: '<div ng-if="row.entity.status == \'待处理\'">'
                    + '<a class="fa fa-edit m-l-xs  primary text-info" title="编辑" ng-click="grid.appScope.editCspOrder(row.entity)" ></a>'
                    + '</div>'
    
                    +'<div ng-if="row.entity.status == \'已处理\' || row.entity.status == \'订单终结\'">'
                    + '<a class="fa fa-eye m-l-xs  primary text-info" title="查看" ng-click="grid.appScope.editCspOrder(row.entity)" ></a>'
                    + '</div>',

                    enableColumnMenu: false, enableSorting: false, width: '4%'
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
            }
        };
        this.$scope.gridOptions.multiSelect = false;
        this.query();
        this.loadOrderStatusDropDown();
    }

    localHistory():void{
        super.localHistory(this.$state)
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    loadOrderStatusDropDown(): void {
        this.$scope.orderStatusDropDown = this.valueService.getCspStatus().data;
    }
    /**
     * 监听野狗事件
     */
    onWilddogOrderEvent = (event: ng.IAngularEvent, data: any) => {
        this.query();
    }

    loadData(queryTerms: ICspOrderQueryTerms, skip: number, count: number): void {
        this.cspOrderService.cspOrderlist(queryTerms.orderId, queryTerms.clientName, queryTerms.states,
            queryTerms.startTime, queryTerms.endTime, queryTerms.startAddress,queryTerms.endAddress, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                this.$scope.heightA = ((result.data.data.length * 39) + 95);
                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 编辑订单
     */
    editCspOrder = (row: cspOrderListResponse) => {
        if(row.status == "待处理"){ 
             this.$state.go("app.order.external_order_edit", { id: row.id });
        }else{
             this.$state.go("app.order.external_orderEdit_Disabel", { id: row.id });
        }
    }
    /**
     * 导出报表
     */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportCspOrderlist(this.$scope.queryTerms.orderId, this.$scope.queryTerms.clientName, this.$scope.queryTerms.states,
                this.$scope.queryTerms.startTime, this.$scope.queryTerms.endTime, this.$scope.queryTerms.startAddress,this.$scope.queryTerms.endAddress)
        }
        
    }
}

angular.module("tms").controller("CspOrderListController", ["$scope","cspOrderService","$state", "$ngBootbox", "valueService","$location","exportService", CspOrderListController]);